package personal.xjl.studentdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import personal.xjl.studentdemo.dao.TeacherMapper;
import personal.xjl.studentdemo.entity.Student;
import personal.xjl.studentdemo.entity.Teacher;
import personal.xjl.studentdemo.service.impl.TeacherServiceImpl;


import java.util.List;

@Controller        //默认对象的名字是首写字母的小写teacherController
@RequestMapping("Teacher")
public class TeacherController {
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    private TeacherServiceImpl teacherServiceImpl;

    @RequestMapping("delete.do")
       public String deleteTeacher(@RequestParam("id") int id) {
        teacherMapper.deleteByPrimaryKey(id);
        return "redirect:/Teacher/list.do";
    }
    @RequestMapping("list.do")
    public String listTeacher(Model model,@RequestParam(value = "currPage",defaultValue = "1")int currPage,@RequestParam(value = "pageSize",defaultValue = "3")int pageSize) {
        List<Teacher> teachers=teacherServiceImpl.selectByPage((currPage-1<0)?0:(currPage-1)*pageSize,pageSize);
        model.addAttribute("teachers",teachers);
        model.addAttribute("currPage",currPage);
        return "listTeachers";
    }
    @RequestMapping("add.do")
    public String addTeacher(Model model,Teacher teacher) {
        teacherServiceImpl.add(teacher);
        return  "redirect:/Teacher/list.do";
    }
    //准备修改教师信息
    @RequestMapping("toEdit.do")
    public String edit(@RequestParam("id") int id, Model model) {
        Teacher oldTeacher=teacherServiceImpl.selectById(id);
        model.addAttribute("teacher",oldTeacher);
        return "editTeacher";
    }
    //修改教师信息
    @RequestMapping("edit.do")
    public String edit(Teacher teacher) {
        teacherServiceImpl.edit(teacher);
        return "redirect:/Teacher/list.do";
    }
}
